package com.fm.financemanage.controller;

import com.fm.financemanage.component.TokenComponent;
import com.fm.financemanage.constant.ResponseEnum;
import com.fm.financemanage.exception.ParameterException;
import com.fm.financemanage.model.po.Accountant;
import com.fm.financemanage.model.po.SysUser;
import com.fm.financemanage.model.vo.PageVo;
import com.fm.financemanage.model.vo.equipment.BusinessEquipmentListVo;
import com.fm.financemanage.model.vo.equipment.BusinessEquipmentOperatorVo;
import com.fm.financemanage.model.vo.simcard.SimCardListVo;
import com.fm.financemanage.model.vo.simcard.SimCardOpeartorVo;
import com.fm.financemanage.response.Callback;
import com.fm.financemanage.response.JsonData;
import com.fm.financemanage.service.EquipmentService;
import com.fm.financemanage.service.SimCardService;
import com.fm.financemanage.util.SysAdminUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;

@RequestMapping("/equipment")
@Api(tags = "企业设备")
@RestController
@Validated
public class EquipmentController {

    @Autowired
    private EquipmentService equipmentService;

    @Autowired
    private TokenComponent tokenComponent;

    @GetMapping("/")
    @ApiOperation("查询列表")
    public JsonData<PageVo<List<BusinessEquipmentListVo>>> getList(HttpServletRequest request, HttpServletResponse response,
                                                                   @ApiParam(name = "accountantId", value = "会计员Id", required = true)
                                                                   @RequestParam(name = "accountantId", required = false) Integer accountantId,
                                                                   @ApiParam(name = "number", value = "设备号", required = true)
                                                                   @RequestParam(name = "number", required = false) String number,
                                                                   @ApiParam(name = "onLineState", value = "在线状态 -1 全部 0 离线 1 在线", required = true)
                                                                   @RequestParam(name = "onLineState", required = false) Integer onLineState,
                                                                   @ApiParam(name = "page", value = "页码", required = true)
                                                                   @RequestParam(name = "page", required = false) Integer page,
                                                                   @ApiParam(name = "limit", value = "条数", required = true)
                                                                   @RequestParam(name = "limit", required = false) Integer limit
    ) {
        Long id = tokenComponent.getUserId(request);

        return Callback.success(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMemo(), equipmentService.getList(id, number, accountantId, onLineState, page, limit));
    }


    @PostMapping("/")
    @ApiOperation("")
    public JsonData operator(HttpServletRequest request, HttpServletResponse response,
                             @RequestBody BusinessEquipmentOperatorVo equipmentOperatorVo, BindingResult bindingResult
    ) {
        if (bindingResult.hasErrors()) {
            throw new ParameterException(bindingResult.getAllErrors());
        }
        final Long userId = tokenComponent.getUserId(request);
        equipmentOperatorVo.setUserId(userId);
        equipmentService.operator(equipmentOperatorVo);
        return Callback.success(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMemo());
    }


    @DeleteMapping("/{uuid}")
    @ApiOperation("删除")
    public JsonData removeIds(HttpServletRequest request, HttpServletResponse response,
                              @PathVariable("uuid") String uuid
    ) {
        equipmentService.removeById(uuid);
        return Callback.success(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMemo());
    }


    @PostMapping("/bind/{equUuid}")
    @ApiOperation("绑定会计")
    public JsonData bindAccountant(HttpServletRequest request, HttpServletResponse response,
                                   @ApiParam(name = "accountantUuids", value = "会计员Uuids", required = false)
                                   @RequestParam(name = "accountantUuids", required = false) String accountantUuids,
                                   @ApiParam(name = "equUuid", value = "设备Id", required = true)
                                   @PathVariable("equUuid") @NotNull(message = "设备uuid不能为空") String equUuid
    ) {

        final Long userId = tokenComponent.getUserId(request);
        equipmentService.bindAccountant(accountantUuids, equUuid,userId);
        return Callback.success(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMemo());
    }



}
